<template>
	<view class="container">
		<uni-section title="用户信息" type="line">
			<uni-group>
				<uni-list>
					<uni-list-item :title="currentUserInfo.last_name"></uni-list-item>
				</uni-list>
				<button type="warn" @click="logout">注销账号</button>
			</uni-group>
		</uni-section>

		<uni-section title="档案下载" type="line">
			<uni-group>
				<uni-list>
					<uni-list-item v-for="(item, index) in mesBasedocArrs" :key="index" :title="item.text">
						<template v-slot:footer>
							<button class="mini-btn" style="margin-right: 10rpx" type="primary" @click="downloadOneMes2Doc(item)" size="mini">下载</button>
							<button class="mini-btn" type="primary" @click="manager(item)" size="mini">查看</button>
						</template>
					</uni-list-item>
				</uni-list>
			</uni-group>
			<view class="" style="padding: 5px">
				<button type="primary" @click="downloadAllMes2Doc(0)">下载全部</button>
			</view>
		</uni-section>
		<uni-section title="默认设置" type="line">
			<uni-group title="设置默认组织">
				<view>
					<select-ref ref="orgs" placeholder="请输入" v-model="defaultDataModel.orgs" :docType="BASEDOC.ORG"></select-ref>
				</view>
			</uni-group>
			<view style="padding: 10rpx">
				<button class="" type="primary" @click="setDefaultDataEvent">确定</button>
			</view>
		</uni-section>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import { mesBasedocArrs, downloadOneMes2Doc, downloadAllMes2Doc } from './basedoc.js';
import { BASEDOC, BASEDOC_DEFAULT_KEY, LOGININFO_KEY } from '@/common/const/index';

export default {
	data() {
		return {
			BASEDOC: BASEDOC,
			mesBasedocArrs,mesBasedocArrs,
			defaultDataModel: {
				stockorg: '',
				qccenterorg: '',
				printnum: '',
				uploadpoint: ''
			},
			uploadpointData: [],
			currentUserInfo: {}
		};
	},
	methods: {
		downloadOneMes2Doc, 
		downloadAllMes2Doc,
		
		manager(item) {
			let basedocKey = item.key;
			uni.navigateTo({
				url: './manager?basedocKey=' + basedocKey
			});
		},
		/**
		 * 退出登录
		 */
		logout() {
			uni.removeStorageSync(this.$GC.LOGININFO_KEY);
			uni.removeStorageSync(this.$GC.ACCESS_TOKEN_KEY);

			uni.showToast({
				title: '退出成功！',
				duration: 1000
			});
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/sys/login/login'
				});
			}, 1000);
		},
		
		/**
		 * 设置默认值确定按钮事件
		 */
		setDefaultDataEvent() {
			let stockorg = this.$refs.stockorg.getRefValue();
			let qccenterorg = this.$refs.qccenterorg.getRefValue();
			let form = uni.$u.deepClone(this.defaultDataModel);
			form['stockorg'] = stockorg;
			form['qccenterorg'] = qccenterorg;
			for (let key in form) {
				uni.setStorage({
					key: BASEDOC_DEFAULT_KEY[key],
					data: form[key]
				});
			}
			this.$msg('设置成功');
		}
	},
	created() {},
	mounted() {},
	onLoad(params) {
		// 获取上一个页面传过来的参数
		this.status = params.basedocKey;

		let that = this;
		// 缓存用户信息
		uni.getStorage({
			key: that.$GC.LOGININFO_KEY,
			success: function (res) {
				that.currentUserInfo = res.data;
			}
		});

		// 读取设置的默认值
		for (let key in this.defaultDataModel) {
			let defaultData = uni.getStorageSync(BASEDOC_DEFAULT_KEY[key]);
			this.defaultDataModel[key] = defaultData;
		}
	}
};
</script>

<style lang="scss">
.mini-btn {
	font-size: 16rpx;
}
.container {
	font-family: 'PingFang SC Regular';
}
</style>
